<?php
namespace app\home\controller;
use app\common\controller\AuthBase;
use myclass\Imageup;

/**
 * @title 上传接口
 * @description 接口说明
 * @group 全局
 */
class Upload extends AuthBase{
    protected $middleware = ['auth'];

    protected function upload($path='',$id=0,$rename=0){
        $base64 = input('base64','');
        $path = $path ? $path:'hs_system/'.request()->action();
        $img = new Imageup($path);

        if($base64){
            $info = $img->base64_image_save($base64,$path,request()->action().time());
        }else{
            $info = $img->upload_img(0,$rename);
        }

        $info['keyid'] = $id;
        return json($info);
    }
    protected function uploadAll($path=''){
        $path = $path ? $path:'hs_system/'.request()->action();
        $Img = new Imageup($path);
        $url = $Img->upload_all_img($path);
        if($url){
            $msg = '上传成功';
            $code = 0;
        }else{
            $msg = '上传失败';
            $code =1;
        }
        return json(['code'=>$code,'msg'=>$msg,'url'=>$url]);
    }
    /**
     * @title 上传
     * @description 接口说明
     * @author 开发者[lonnie]
     * @url /admin/base.Upload/teacher
     * @method POST
     *
     * @param name:teacher type:string require:0 default:0 other: desc:讲师
     * @param name:course type:string require:0 default:0 other: desc:课程
     * @param name:cases type:string require:0 default:0 other: desc:案例
     * @param name:activity type:string require:0 default:0 other: desc:活动
     * @param name:project type:string require:0 default:0 other: desc:项目
     * @param name:news type:string require:0 default:0 other: desc:新闻
     * @param name:video type:string require:0 default:0 other: desc:视频
     * @param name:video type:string require:0 default:0 other: desc:视频
     *
     * @return url:url
     **/

    public function teacher(){
        $teacherid = input('img');
        $path = "hs_system/need/".$teacherid;
        return $this->upload($path);
    }
    /**
     * @title 线索上传(多个)
     * @description 接口说明
     * @author 开发者[ljl]
     * @url /admin/base.Upload/clue
     * @method POST
     *
     * @param name:id type:string require:0 default:0 other: desc:线索id
     * @param name:file type:array require:0 default:0 other: desc:合同文件
     *
     * @return url:数组类型
     **/
    //线索
    public function clue(){
        $id = getInput('id','other');
        return $this->uploadAll("newhdp/clue/$id");
    }
    //案例
    public function cases(){
        $teacherid = input('teacherid');
        $casesid = input('keyid');
        $path = "newhdp/teacher/".$teacherid.'/cases/'.$casesid;
        return $this->upload($path,$casesid);
    }
    //活动
    public function activity(){
      return $this->upload();
    }

    //新闻
    public function news(){
        return $this->upload();
    }
    //项目
    public function project(){
        $path = "hs_system/need/";
        return $this->upload();
    }

    //bug
    public function bug(){
        $path = "hs_system/bug/";
        $file = request()->file('img');
        $bef_info=$file->getInfo();
        $tmp_name_arr=explode(".",$bef_info["name"]);
        if (!in_array(end($tmp_name_arr),['jpg','jpeg','png','gif'])) {
            return json(['code'=>1,"msg"=>"只能上传图片","size"=>$bef_info['size']]);
        }
        return $this->upload();
    }
    //课程
    public function course(){
        return $this->upload();
    }
    //视频
    public function video(){
        return $this->upload();
    }


    /**
     * 上传无底形象照
     */
    public function upload_figure() {
        if (\request()->isPost()) {
            $auth = $this->auth_check("admin/teacher.manage/upload_figure_auth");
            if (!$auth) {
                return json(['code'=>1,'msg'=>'没有权限上传无底形象照']);
            }
            $param = input();
            if (!isset($param['teacherid']) || empty($param['teacherid'])) {
                return json(['code'=>1,'msg'=>'数据格式teacherid错误']);
            }

            $path = "newhdp/teacher_bg_png/";
            return $this->upload($path,0,1);
        }
    }
}